package com.sbeam.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sbeam.mapper.FactoryMapper;
import com.sbeam.mapper.GamesMapper;
import com.sbeam.pojo.Factory;
import com.sbeam.pojo.Games;
import com.sbeam.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FactoryServiceImpl implements FactoryService{

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private GamesMapper gamesMapper;

    @Override
    public List<Factory> getFactoryList() {
        List<Factory> factoryList = factoryMapper.getFactoryList();
        //循环遍历厂商数据，为其封装游戏数据
        for (Factory factory:factoryList){
            Integer factoryId = factory.getId();
            List<Games> gamesList = gamesMapper.getGamesByFactoryId(factoryId);
            factory.setChildren(gamesList);
        }
        return factoryList;
    }

    @Override
    public List<Factory> findFactoryByName(String searchKey) {
        Map map = new HashMap();
        map.put("searchKey",searchKey);
        map.put("start",0);
        map.put("pageSize",999);
        List<Factory> factoryList = factoryMapper.findFactoryByName(map);
        if (factoryList.isEmpty()) {
            return factoryList;
        }
        for (Factory factory:factoryList){
            Integer factoryId = factory.getId();
            List<Games> gamesList = gamesMapper.getGamesByFactoryId(factoryId);
            factory.setChildren(gamesList);
        }
        return factoryList;
    }

    @Override
    public int addFactory(Factory factory) {
        factory.setStatus(true)
                .setCreated(new Date())
                .setUpdated(new Date());
        return factoryMapper.addFactory(factory);
    }

    /**
     * 不仅需要更新厂商的状态，还需要同时将厂商旗下所有的游戏状态也进行更新
     * @param factory
     * @return
     */
    @Override
    public int updateFactoryStatus(Factory factory) {
        Date updated = new Date();
        factory.setUpdated(updated);
        List<Games> gamesList = gamesMapper.getGamesByFactoryId(factory.getId());
        if (gamesList!=null&&!gamesList.isEmpty()){
            for (Games games:gamesList){
                games.setStatus(factory.getStatus())
                        .setUpdated(updated);
                gamesMapper.updateGamesStatus(games);
            }
        }
        return factoryMapper.updateFactoryStatus(factory);
    }

    @Override
    public int updateFactory(Factory factory) {
        factory.setUpdated(new Date());
        return factoryMapper.updateFactory(factory);
    }

    /**
     * 不仅要删除该厂商，还应该连同该厂商旗下的游戏一起删除
     * @param id
     * @return
     */
    @Override
    public int deleteFactoryById(Integer id) {
        List<Games> gamesList = gamesMapper.getGamesByFactoryId(id);
        if (gamesList!=null&&!gamesList.isEmpty()){
            for (Games games:gamesList){
                gamesMapper.deleteGamesById(games.getId());
            }
        }
        return factoryMapper.deleteFactoryById(id);
    }
    @Override
    public PageResult getFactory(PageResult pageResult) {
        //判断数据是否有值
        QueryWrapper<Factory> queryWrapper = new QueryWrapper<>();
        //如果查询条件有值，返回true；无值返回false
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        //flag为true，按查询条件执行；false则查询所有数据
        queryWrapper.like(flag, "factory_name",pageResult.getQuery());
        //编辑MP的分页对象   四个属性有用（页数/条数/总数/记录）   传递=页数/条数
        //创建page对象，并传入当前页数与每页条数，利用MP提供的工具API进行页面封装
        IPage<Factory> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        //调用MP的selectPage方法，传入相应参数，获取封装好的page对象
        page = factoryMapper.selectPage(page, queryWrapper);
        //获取总数
        long total = page.getTotal();
        //获取记录数
        List<Factory> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }
}
